import {saveSysBuryingPoint} from '@/api/system/sysBuryingPointApi';
import {reqCommonMsg} from '@/util/reqCommonMsg';
import console from '@/util/console'
import {getStore} from '@/util/store'

/**
 * 保存埋点
 */
export function buryingPoint(param,type){
  console.log(param,type);
  let params = {};
  if(type == 'sidebar' ){ //菜单点击
    params.statisticsType = '左侧菜单'
    params.menuId = param.id;
    params.menuName = param.name;
    params.menuCode = param.code;
    params.requestAddress = param.path;
    params.parentMenuId  = param.parentId;
  }
  else if(type == 'top' ){ //顶部菜单默认进入
    params.statisticsType = '顶部菜单'
    params.menuId = 'top1001';
    params.menuName = param.label;
    params.menuCode = 'none';
    params.requestAddress = param.path;
    params.parentMenuId  = 'none';
  }
  else if(type == 'tag' ){ //标签进入
    params.statisticsType = '标签菜单'
    params.menuId = 'tag1001';
    params.menuName = param.label;
    params.menuCode = 'none';
    params.requestAddress = param.name;
    params.parentMenuId  = 'none';
  }
  else if(type == 'self' ){ //自定义链接进入
    params.statisticsType = '自定义菜单'
    params.menuId = param.id;
    params.menuName = param.name;
    params.menuCode = 'none';
    params.requestAddress = param.path;
    params.parentMenuId  = 'none';
  }
  else if(type == 'login' ){ //自定义链接进入
    params.statisticsType = '用户登录'
    params.menuId = 'index';
    params.menuName = '首页';
    params.menuCode = param;
    params.requestAddress = '/index';
    params.parentMenuId  = 'none';
  }
  else{
    params  = param;
  }

  //延迟2秒写入埋点信息
  setTimeout(() => {
    let reqParams =  {
      singleBody:  {
        requestChannel: 'PC',
        statisticsType: params.statisticsType,
        menuId: params.menuId,
        menuName: params.menuName,
        menuCode: params.menuCode,
        requestAddress: params.requestAddress,
        parentMenuId: params.parentMenuId,
        requestRealName: getStore({name: 'userInfo'})? getStore({name: 'userInfo'}).realName : '', 
        requestUserName: getStore({name: 'userInfo'})? getStore({name: 'userInfo'}).userName : '',
        requestDeptName: getStore({name: 'userInfo'})? getStore({name: 'userInfo'}).belongOrgName : '',
      },
    }
    let commonData = reqCommonMsg(reqParams)
    saveSysBuryingPoint(commonData).then(() => {
      console.log('==埋点成功！');
    }, error => {
    }); 
  }, 2000);
}
